package com.xcode.biz.service.impl;

import com.google.common.collect.Lists;
import com.xcode.biz.dto.AddressDTO;
import com.xcode.biz.dto.UserDTO;
import com.xcode.biz.mapper.UserMapper;
import com.xcode.biz.service.AddressService;
import com.xcode.biz.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuziying
 * @Description 用户服务
 * @Date 2020/12/24
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private AddressService addressService;

    @Resource
    private UserMapper userMapper;

    @Override
    public void saveUser(UserDTO userDTO) throws Exception{
        if(ObjectUtils.isEmpty(userDTO.getUserCode())){
            userMapper.saveUser(userDTO);
        }else {
            userMapper.updareUser(userDTO);
        }
    }

    public UserDTO getUserByCode1(String userCode) {
        UserDTO userDTO;
        try{
            userDTO = getUser(userCode);
            AddressDTO addressByUserCode = addressService.getAddressByUserCode(userCode);
            userDTO.setAddressDTO(addressByUserCode);
        }catch (Exception e){
            throw new RuntimeException();
        }
        return userCode == null ? null : userMapper.getUserByCode(userCode);
    }

    @Override
    public UserDTO getUserByCode(UserDTO userDTO) {
        return getUser(userDTO.getUserCode());
    }

    private UserDTO getUser(String userCode){
        return userMapper.getUserByCode(userCode);
    }

    private UserDTO getUser(String userCode,int a){
        return userMapper.getUserByCode(userCode);
    }

    @Override
    public List<UserDTO> getUserList(String userCode,int age) {
        List<UserDTO> userDTOS;
        userDTOS = Lists.newArrayList();
        List<UserDTO> userList = userMapper.getUserList(age);
        switch (age){
            case 1:
                List<UserDTO> userList2 = userMapper.getUserList(age);
                break;
            case 2:
                List<UserDTO> userList1 = userMapper.getUserList(age);
                break;
            default:
                System.out.println("====");
        }
        for(UserDTO userDTO123 : userList){
            AddressDTO addressByUserCode = addressService.getAddressByUserCode(userDTO123.getUserCode());
            userDTO123.setAddressDTO(addressByUserCode);
            userDTOS.add(userDTO123);
        }
        return userDTOS;
    }


}
